package com.itheima.JingTaiDaiMa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;

import static java.lang.Integer.parseInt;

public class TestDemo {
    public static void main(String[] args) throws IOException {
        Map<String, String> map = Student.method();

        map.forEach((String s, String s1)->{
            System.out.print(s+" "+s1);
        });
        System.out.println("----------------");
        Set<String> strings = map.keySet();
      ZhaoGunJun(map);
        ZhaoNianFen(map, strings);

    }

    private static void ZhaoNianFen(Map<String, String> map, Set<String> strings) throws IOException {
        while(true) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入国家(a：结束)：");
            String s = null;
            String s1 = br.readLine();
            if (s1.equals("a")){
                break;

            }
            //先判断是否包含，再遍历，提高程序效率
            if (map.containsValue(s1)) {
                for (String string : strings) {
                    if (map.get(string).equals(s1)) {
                        System.out.println(string);
                    }
                }
            } else {
                System.out.println(s1 + "没有获得过世界杯");
            }
        }
    }

    private static void ZhaoGunJun(Map<String, String> map) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s=null;
        while(true) {
            System.out.println("请输入年份(886:结束)：");
             s = br.readLine();
            System.out.println(s);
             if(s.equals("886")){
                 break;
             }
            int i = 0;
            try {
                i = parseInt(s);
                if (i<=9999&&i>=1000){
                    if (map.containsKey(s)) {
                        String s1 = map.get(s);
                        System.out.println(s + "年的冠军是：" + s1);
                    } else {
                        System.out.println(s + "年没有举办活动");
                    }
                }else{
                    System.out.println("四位数据年份");

                }            } catch (NumberFormatException e) {
                System.out.println("不合法数据");
            }
        }
    }
}
